package cn.kai.org.controller;

import cn.kai.basic.anno.LogAnno;
import cn.kai.basic.anno.PreAuthorize;
import cn.kai.basic.config.BussinessException;
import cn.kai.basic.controller.BaseController;
import cn.kai.basic.query.BaseQuery;
import cn.kai.basic.util.AjaxResult;
import cn.kai.basic.util.PageList;
import cn.kai.org.domain.Department;
import cn.kai.org.service.IDepartmentServiceI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description = "部门相关的CRUD功能")
@PreAuthorize
public class DepartmentController{

    @Autowired
    private IDepartmentServiceI departmentService;

    /**
     * 删除 delete localhost:8080/dept/1
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "部门删除",sn = "department:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.me().success();
        }catch (BussinessException bu){
            bu.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(bu.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请重试!");
        }
    }

    /**
     * 查询所有
     */
    @GetMapping
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public Department findById(@ApiParam(name="主键",required = true) @PathVariable("id") Long id){
        return (Department) departmentService.findById(id);
    }

    /**
     *
     * 分页查询
     */
    @PostMapping
    @ApiOperation(value = "分頁查詢")
    public PageList<Department> queryByPage(@RequestBody BaseQuery query){
        return departmentService.queryByPage(query);
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            departmentService.batchDel(ids);
            return AjaxResult.me().success();
        }catch (BussinessException bu){
            bu.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(bu.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请重试!");
        }
    }

    /**
     * 添加/修改
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (null == department.getId()){
                // 添加
                departmentService.add(department);
            }else {
                // 修改
                departmentService.update(department);
            }
            return AjaxResult.me().success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请重试!");
        }
    }

    /**
     * 无限级数
     */
    @GetMapping("/tree")
    public List<Department> findDeptDepartmentree(){
        return departmentService.findDeptTree();
    }

}
